package org.genntii.warehousesystem.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.Exception.PasswordErrorException;
import org.genntii.warehousesystem.Exception.UsernameExistsException;
import org.genntii.warehousesystem.Exception.UsernameNotFoundException;
import org.genntii.warehousesystem.common.JwtProperties;
import org.genntii.warehousesystem.domain.entity.User;
import org.genntii.warehousesystem.mapper.UserMapper;
import org.genntii.warehousesystem.security.DBUserManager;
import org.genntii.warehousesystem.service.UserService;
import org.genntii.warehousesystem.utils.JwtUtil;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private DBUserManager userManager;

    @Override
    public Map<Object, Object> register(String username, String password) {

        if (userMapper.usernameIsExists(username) >= 1){
            throw new UsernameExistsException("用户名已存在");
        }

        User user = new User();
        user.setUsername(username);
        String salt = BCrypt.gensalt();
        user.setPassword(BCrypt.hashpw(password,salt));
        user.setSalt(salt);
        user.setStatus(1);
        user.setCreateTime(LocalDateTime.now());

        userMapper.insert(user);

        Map<String,Object> claims =Map.of("id",userMapper.selectIdByUsername(username),"username",username);

        String token = JwtUtil.createJWT(jwtProperties.getSecretKey(),jwtProperties.getTtl(),claims );

        return Map.of("message","注册","token",token);
    }

    @Override
    public Map<Object, Object> login(String username, String password) {
        User u = null;
        u = userMapper.selectByUsername(username);
        if (u == null){
            throw new UsernameExistsException("用户不存在");
        }

        if (!BCrypt.checkpw(password,u.getPassword())){
            throw new PasswordErrorException("密码错误");
        }

        Map<String,Object> claims =Map.of("id",userMapper.selectIdByUsername(username),"username",username);

        String token = JwtUtil.createJWT(jwtProperties.getSecretKey(),jwtProperties.getTtl(),claims );

        return Map.of("message",userManager.loadUserByUsername(username),"token",token);

    }

    @Override
    public boolean userIsRealName(int id) {
        return userMapper.userIsRealName(id) != null && !userMapper.userIsRealName(id).isEmpty();
    }

    @Override
    public void realName(String name, String identity, int id) {
        userMapper.realName(name,identity,id,LocalDateTime.now());
    }

    @Override
    public boolean userIsBank(int id) {
        return userMapper.userIsBank(id) != null && !userMapper.userIsBank(id).isEmpty();
    }

    @Override
    public void bank(String account, String name, int id, LocalDateTime time) {
        userMapper.bank(account,name,id,LocalDateTime.now());
    }

    @Override
    public boolean userIsTel(int id) {
        return userMapper.userIsTel(id) != null && !userMapper.userIsTel(id).isEmpty();
    }

    @Override
    public void tel(String tel, LocalDateTime time, int id) {
        userMapper.tel(tel,LocalDateTime.now(),id);
    }

    @Override
    public void setCommonlyAddress(int id, int AddressId) {
        userMapper.addressUpdate(AddressId,id);
    }
}
